﻿using DevExpress.XtraGrid.Views.Base;
using MM.Commmon.UI;
using MM.Common.Base;
using MM.Common.DBBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmSelectFunction : FrmBase
    {
        private FunctionPara funPara;
        public FrmSelectFunction()
        {
            InitializeComponent();
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.Cancel;
            base.Close();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.No;
            base.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DataRow focusedDataRow = this.gvFunction.GetFocusedDataRow();
            if (focusedDataRow != null)
            {
                this.funPara.pCode = focusedDataRow["FUNCTION_CODE"].ToString();
                this.funPara.pName = focusedDataRow["FUNCTION_NAME"].ToString();
                base.DialogResult = DialogResult.OK;
                base.Close();
            }
        }
        private void FrmSelectFunction_Shown(object sender, EventArgs e)
        {
            string oldModuleCode = "";
            if (this.funPara.pCode != null)
            {
                oldModuleCode = this.GetOldModuleCode();
            }
            int num = -1;
            DataTable dt = new DataTable();
            SystemInfo.DbData.Retrieve("SELECT MODULE_CODE ,MODULE_NAME ,MODULE_FILENAME ,FILE_TIME FROM MXHLY_SYS_MODULE WHERE USED_FLAG = 'Y'ORDER BY MODULE_CODE ", ref dt);
            this.gridControl1.DataSource = dt;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["MODULE_CODE"].ToString() == oldModuleCode)
                {
                    num = i;
                    break;
                }
            }
            if (num >= 0)
            {
                this.gvModule.FocusedRowHandle = num;
                for (int j = 0; j < this.gvFunction.RowCount; j++)
                {
                    if (this.gvFunction.GetRowCellValue(j, "FUNCTION_CODE").ToString() == this.funPara.pCode)
                    {
                        this.gvFunction.FocusedRowHandle = j;
                        return;
                    }
                }
            }
        }

        private string GetOldModuleCode()
        {
            string str = "";
            object obj2 = null;
            try
            {
                SystemInfo.DbData.ExecuteScalar("SELECT MODULE_CODE FROM MXHLY_SYS_FUNCTION WHERE FUNCTION_CODE = :fcode", new DataParamCol(new DataParam[] { new InputDataParam("fcode", this.funPara.pCode) }), ref obj2);
                str = obj2.ToString();
            }
            catch (Exception)
            {
            }
            return str;
        }

        private void gvModule_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle >= 0)
            {
                string paramValue = this.gvModule.GetFocusedDataRow()["MODULE_CODE"].ToString();
                DataTable dt = new DataTable();
                string commandtext = "SELECT FUNCTION_CODE, FUNCTION_NAME, OBJECT_NAME, PARAMETER \r\n   FROM MXHLY_SYS_FUNCTION \r\n    WHERE MODULE_CODE = :modulecode AND USED_FLAG = 'Y' \r\n   ORDER BY FUNCTION_CODE ASC ";
                DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("modulecode", paramValue) });
                SystemInfo.DbData.Retrieve(commandtext, parm, ref dt);
                this.gridControl2.DataSource = dt;
            }
        }

        public bool SetParameter(FunctionPara fpara)
        {
            if (this.Init())
            {
                this.funPara = fpara;
                return true;
            }
            return false;
        }
    }
}
